home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / rcs55.zip / GETCWD.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  1KB  |  53 lines

  1. // Alternative getcwd() for use with RCS as ported to Borland C++
  2. //
  3. // Returns path elements seperated by '/' rather than '\'
  4. //
  5.  
  6. #include <dir.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11.  
  12. static char RCSid[] = "$Id: getcwd.c%v 1.2 1991/08/23 13:26:54 SGP Exp $";
  13.  
  14. char *getcwd(char *buf, int buflen)
  15. {
  16.     char buffer[MAXDIR+3];
  17.     char *s, *d, *rbuf = buf;
  18.     int drive = getdisk();
  19.  
  20.     if (buf == NULL){
  21.         // malloc a buffer for the return value if one isn't supplied
  22.         if ((rbuf = (char *)malloc(MAXDIR+3)) == NULL){
  23.             errno = ENOMEM;
  24.             return (NULL);
  25.         }
  26.     }
  27.  
  28.     if (getcurdir(0,buffer) < 0){
  29.         // Shouldn't fail - maybe no such device ?!?!?
  30.         errno = ENODEV;
  31.         return (NULL);
  32.     }
  33.  
  34.     if (buflen-3 < (int)strlen(buffer)){
  35.         // No room in the return buffer
  36.         errno = ERANGE;
  37.         return (NULL);
  38.     }
  39.  
  40.     // Set up the return value
  41.  
  42.     rbuf[0] = tolower('A' + drive);
  43.     rbuf[1] = ':';
  44.     rbuf[2] = '/';
  45.  
  46.     for (s = buffer, d = &rbuf[3]; *s != '\0'; s++)
  47.         *d++ = *s != '\\' ? tolower(*s) : '/';
  48.  
  49.     *d = '\0';
  50.     errno = 0;
  51.     return (rbuf);
  52. }
  53.